package com.tianjin.weight.controller.sys;


import com.tianjin.weight.aop.annotation.LogAnnotation;
import com.tianjin.weight.behaviour.neutral.PermissionWithNeutralBehavior;
import com.tianjin.weight.entity.Ao;
import com.tianjin.weight.entity.Page;
import com.tianjin.weight.entity.SysPermission;
import com.tianjin.weight.model.pc.PermissionWithPing;
import com.tianjin.weight.service.base.PermissionService;
import com.tianjin.weight.service.ping.PermissionWithPingService;
import com.tianjin.weight.utils.DataResult;
import com.tianjin.weight.utils.JwtTokenUtil;
import com.tianjin.weight.vo.req.PermissionAddReqVO;
import com.tianjin.weight.vo.req.PermissionUpdateReqVO;
import com.tianjin.weight.vo.resp.PageVO;
import com.tianjin.weight.vo.resp.PermissionRespNode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName: PermissionController
 * TODO:类文件简单描述
 * @Author: 小霍
 * @CreateDate: 2019/9/20 14:05
 * @UpdateUser: 小霍
 * @UpdateDate: 2019/9/20 14:05
 * @Version: 0.0.1
 */
@RequestMapping("/sys")
@RestController
@Api(tags = "组织模块-菜单权限管理")
public class SysPermissionController {
    @PostMapping("/ping/permission")
    @ApiOperation(value = "新增菜单权限接口")
    @LogAnnotation(title = "菜单权限管理",action = "新增菜单权限")
    @RequiresPermissions("sys:ping:permission:insert")
    public DataResult<SysPermission> addPermission(@RequestBody @Valid PermissionAddReqVO vo) throws Exception {
        DataResult<SysPermission> result=DataResult.success();
        PermissionService permissionService =  new PermissionWithPingService(
                new PermissionWithPing(
                        vo,
                        null,
                        Ao.builder().build()
                ),
                new PermissionWithNeutralBehavior()
        );
        permissionService.add();
        result.setData(null);
        return result;
    }
    @DeleteMapping("/ping/permission/{id}")
    @ApiOperation(value = "删除菜单权限接口")
    @LogAnnotation(title = "菜单权限管理",action = "删除菜单权限")
    @RequiresPermissions("sys:ping:permission:delete")
    public DataResult delete(@PathVariable("id") Long id) throws Exception {
        DataResult result=DataResult.success();
        PermissionService permissionService =  new PermissionWithPingService(
                new PermissionWithPing<>(
                        null,
                        null,
                        Ao.builder()
                        .id(id)
                        .build()
                ),
                new PermissionWithNeutralBehavior()
        );
        permissionService.delete();
        return result;
    }
    @PutMapping("/ping/permission")
    @ApiOperation(value = "更新菜单权限接口")
    @LogAnnotation(title = "菜单权限管理",action = "更新菜单权限")
    @RequiresPermissions("sys:ping:permission:update")
    public DataResult update(@RequestBody @Valid PermissionUpdateReqVO vo) throws Exception {
        DataResult result=DataResult.success();
        PermissionService permissionService =  new PermissionWithPingService(
                new PermissionWithPing<>(
                        vo,
                        null,
                        Ao.builder()
                            .id(vo.getId())
                            .build()
                ),
                new PermissionWithNeutralBehavior()
        );
        permissionService.update();
        return result;
    }
    @GetMapping("/ping/permission/{id}")
    @ApiOperation(value = "查询菜单权限接口")
    @LogAnnotation(title = "菜单权限管理",action = "查询菜单权限")
    @RequiresPermissions("sys:ping:permission:one")
    public DataResult<SysPermission> getOne(@PathVariable("id") Long id) throws Exception {
        DataResult<SysPermission> result=DataResult.success();
        PermissionService permissionService =  new PermissionWithPingService(
                new PermissionWithPing<>(
                        null,
                        null,
                        Ao.builder()
                                .id(id)
                                .build()
                ),
                new PermissionWithNeutralBehavior()
        );
        result.setData((SysPermission) permissionService.getOne());
        return result;
    }

    @PostMapping("/ping/permissions")
    @ApiOperation(value = "分页查询菜单权限接口")
    @LogAnnotation(title = "菜单权限管理",action = "分页查询菜单权限")
    @RequiresPermissions("sys:ping:permission:list")
    public DataResult<PageVO<SysPermission>> getList(@RequestBody Page vo) throws Exception {
        DataResult<PageVO<SysPermission>> result=DataResult.success();
        PermissionService permissionService =  new PermissionWithPingService(
                new PermissionWithPing<>(
                        null,
                        vo,
                        null
                ),
                new PermissionWithNeutralBehavior()
        );
        result.setData(permissionService.getList());
//        result.setData(permissionService.pageInfo(vo));
        return result;

    }
    @GetMapping("/ping/permission")
    @ApiOperation(value = "获取所有菜单权限接口")
    @LogAnnotation(title = "菜单权限管理",action = "获取所有菜单权限")
    @RequiresPermissions("sys:ping:permission:list")
    public DataResult<List<SysPermission>> getAll() throws Exception {
        DataResult<List<SysPermission>> result=DataResult.success();
        PermissionService permissionService =  new PermissionWithPingService(
                new PermissionWithPing<>(
                        null,
                        null,
                        null
                ),
                new PermissionWithNeutralBehavior()
        );
        result.setData(permissionService.getAll());
//        result.setData(permissionService.selectAll());
        return result;
    }
//    @GetMapping("/ping/permissions")
//    @ApiOperation(value = "获取所有菜单权限接口")
//    @LogAnnotation(title = "菜单权限管理",action = "获取所有菜单权限")
//    @RequiresPermissions("sys:ping:permission:list")
//    public DataResult<List<SysPermission>> getAllMenusPermission(){
//        DataResult<List<SysPermission>> result=DataResult.success();
//        result.setData(permissionService.selectAll());
//        return result;
//    }
    @GetMapping("/ping/permission/tree")
    @ApiOperation(value = "获取所有目录菜单树接口")
    @LogAnnotation(title = "菜单权限管理",action = "获取所有目录菜单树")
    @RequiresPermissions(value = {"sys:ping:permission:update","sys:ping:permission:insert"},logical = Logical.OR)
    public DataResult<List<PermissionRespNode>> getAllMenusPermissionTree(@RequestParam(required = false) Long permissionId) throws Exception {
        DataResult<List<PermissionRespNode>> result=DataResult.success();
        PermissionService permissionService =  new PermissionWithPingService(
                new PermissionWithPing<>(
                        null,
                        null,
                        null
                ),
                new PermissionWithNeutralBehavior()
        );
        result.setData(permissionService.selectAllMenuByTree(permissionId));
//        result.setData(permissionService.selectAllMenuByTree(permissionId));
        return result;
    }

    @GetMapping("/ping/permission/tree/all")
    @ApiOperation(value = "获取所有目录菜单树接口")
    @LogAnnotation(title = "菜单权限管理",action = "获取所有目录菜单树")
    //@RequiresPermissions(value = {"sys:role:update","sys:role:add"},logical = Logical.OR)
    public DataResult<List<PermissionRespNode>> getAllPermissionTree() throws Exception {
        DataResult<List<PermissionRespNode>> result=DataResult.success();
        PermissionService permissionService =  new PermissionWithPingService(
                new PermissionWithPing<>(
                        null,
                        null,
                        null
                ),
                new PermissionWithNeutralBehavior()
        );
        result.setData(permissionService.selectAllByTree());
//        result.setData(permissionService.selectAllByTree());
        return result;
    }

    /**
     * 厂商端获取所有的菜单目录
     */
    @GetMapping("/ping/permission/tree/platformAll")
    @ApiOperation(value = "获取所有目录菜单树接口")
    @LogAnnotation(title = "菜单权限管理",action = "获取所有目录菜单树")
    @RequiresPermissions(value = {"sys:ping:role:update","sys:ping:role:insert"},logical = Logical.OR)
    public DataResult<List<PermissionRespNode>> platformSelectAllByTree(HttpServletRequest request) throws Exception {
        String accessToken=request.getHeader("authorization");
        Long userId= JwtTokenUtil.getUserId(accessToken);
        DataResult<List<PermissionRespNode>> result=DataResult.success();
        PermissionService permissionService =  new PermissionWithPingService(
                new PermissionWithPing<>(
                        null,
                        null,
                        null
                ),
                new PermissionWithNeutralBehavior()
        );
        result.setData(permissionService.platformSelectAllByTree(userId));
//        result.setData(permissionService.platformSelectAllByTree(userId));
        return result;
    }

    /**
     * 平台端
     * @return
     */
    @GetMapping("/platformPermissions")
    @ApiOperation(value = "获取所有菜单权限接口")
    @LogAnnotation(title = "菜单权限管理",action = "获取所有菜单权限")
    @RequiresPermissions("sys:ping:permission:list")
    public DataResult<List<SysPermission>> getAllMenusPermissionPlatform(HttpServletRequest request) throws Exception {
        String accessToken=request.getHeader("authorization");
        Long userId= JwtTokenUtil.getUserId(accessToken);
        DataResult<List<SysPermission>> result=DataResult.success();
        PermissionService permissionService =  new PermissionWithPingService(
                new PermissionWithPing<>(
                        null,
                        null,
                        null
                ),
                new PermissionWithNeutralBehavior()
        );
        result.setData(permissionService.selectAllPlatform(userId));
//        result.setData(permissionService.selectAllPlatform(userId));
        return result;
    }

}
